﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using Web.Context.Analysis.Entities;

namespace Web.Context.Analysis
{
    public class SettingHelper
    {
        public const string VALUE = "Value",
                            MEMO = "Memo",
                            NAME = "Name",
                            VAL_PROPERTY = "ValProperty",
                            ELEMENT_NAME = "ElementName",
                            ELEMENT_CONTEXT_STRING = "ElementContextString",
                            ELEMENT_CLASS = "ElementClass",ELEMENT_ID="ElementId",
                            SETTINGS = "Settings",
                            URL = "Url",
                            KEY = "Key",
                            CONTEXT = "Context",
                            ROW = "Row",
                            ITEM = "Item";

        public List<ContextNode> m_ContextNodes = new List<ContextNode>();
        private string m_FilePath = "ContextAnalysisSetting.xml";
        public List<ItemNode> m_ItemNodes = new List<ItemNode>();
        public List<RowNode> m_RowNodes = new List<RowNode>();
        public List<UrlNode> m_UrlNodes = new List<UrlNode>();

        public SettingHelper()
        {
            Init();
        }

        public static string GetArrValue(XmlNode node, string strAttrName)
        {
            return node.Attributes != null && node.Attributes[strAttrName]!=null ? node.Attributes[strAttrName].Value : "";
        }

        protected void InitRows(XmlElement parentXmlNode, ContextNode parent)
        {
            if (parentXmlNode == null || parent == null) return;
            foreach (XmlElement n in parentXmlNode.ChildNodes)
            {
                var rowNode = new RowNode(n, parent);
                m_RowNodes.Add(rowNode);
                InitItems(n, rowNode);
            }
        }

        protected void InitItems(XmlElement parentXmlNode, RowNode parent)
        {
            if (parentXmlNode == null || parent == null) return;
            foreach (XmlNode n in parentXmlNode.ChildNodes)
            {
                var itemNode = new ItemNode(n, parent);
                if(itemNode.Name.Length > 0)
                    m_ItemNodes.Add(itemNode);
            }
        }

        protected void InitContexts(XmlElement parentXmlNode, UrlNode parent)
        {
            if (parentXmlNode == null || parent == null) return;
            foreach (XmlElement n in parentXmlNode.ChildNodes)
            {
                var contextNode = new ContextNode(n, parent);
                m_ContextNodes.Add(contextNode);
                InitRows(n, contextNode);
            }
        }

        protected void Init()
        {
            m_UrlNodes.Clear();
            m_ItemNodes.Clear();
            m_RowNodes.Clear();
            m_ContextNodes.Clear();
            var doc = new XmlDocument();
            doc.Load(m_FilePath);
            XmlNodeList nodes = doc.SelectNodes("Settings/" + URL);
            if (nodes == null) return;
            foreach (XmlElement n in nodes)
            {
                var urlNode = new UrlNode(n);
                m_UrlNodes.Add(urlNode);
                InitContexts(n, urlNode);
            }
        }

        public List<HtmlElement> GetElements(HtmlElement eHtml, SettingNode node)
        {
            var hes = eHtml.GetElementsByTagName(node.ElementName);
            return GetElements(hes, node);
        }

        public List<HtmlElement> GetElements(HtmlDocument eHtml, SettingNode node)
        {
            var hes = eHtml.GetElementsByTagName(node.ElementName);
            return GetElements(hes, node);
        }

        public List<HtmlElement> GetElements(HtmlElementCollection hes, SettingNode node)
        {
            var reList = new List<HtmlElement>();
            foreach (HtmlElement he in hes)
            {
                if (!string.IsNullOrEmpty(node.ElementId) && he.Id == node.ElementId)
                    reList.Add(he);
                else if(!string.IsNullOrEmpty(node.ElementClass) && he.GetAttribute("ClassName") == node.ElementClass)
                    reList.Add(he);
                else if(!string.IsNullOrEmpty(node.ElementContextString) && (he.OuterHtml.IndexOf(node.ElementContextString) > -1))
                    reList.Add(he);
            }
            return reList;
        }

        public bool IsKeyInfo(string val,string key)
        {
            if (string.IsNullOrEmpty(key)) return true;
            var ss = key.Split(' ');
            return ss.Any(val.Contains);
        }
    }
}